package arrAndstr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
    399. 除法求值
    有问题 --> 使用并查集
    a:
        a
        2.0*b
    b:
        b
        a/2.0
        3.0*c
    c:
        c
        b/3.0
 */
public class T399 {
    public static double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
        Map<String, List<String>> map = new HashMap<>();

        for (int i = 0; i < equations.size(); i++) {
            List<String> equation = equations.get(i);
            double value = values[i];

            // 加入列表
            for (int j = 0; j < equation.size(); j++) {
                List<String> list;
                if (!map.containsKey(equation.get(j))) {
                    list = new ArrayList<>();
                    list.add(equation.get(j));
                } else {
                    list = map.get(equation.get(j));
                }

                if (j==0)   list.add(value+"*"+equation.get(1));
                else list.add(equation.get(0)+"/"+value);

                map.put(equation.get(j), list);
            }
        }

        double[] result = new double[queries.size()];
        // 开始计算
        for (int i = 0; i < queries.size(); i++) {
            List<String> list = queries.get(i);
            String s1 = list.get(0);
            String s2 = list.get(1);

            if (!map.containsKey(s1) || ! map.containsKey(s2)) {
                result[i] = -1.0;
                continue;
            }

            // 遍历第一个、第二个元素内容，进行处理
            List<String> list1 = map.get(s1);
            List<String> list2 = map.get(s2);
            for (String str1 : list1) {
                String tempStr1;
                double tempDouble1;
                if (str1.contains("/")) {
                    tempStr1 = str1.split("/")[0];
                    tempDouble1 = Double.parseDouble(str1.split("/")[1]);
                } else if (str1.contains("*")) {
                    tempStr1 = str1.split("\\*")[1];
                    tempDouble1 = Double.parseDouble(str1.split("\\*")[0]);
                } else {
                    tempStr1 = str1;
                    tempDouble1 = 0.0;
                }
                for (String str2 : list2) {
                    String tempStr2;
                    double tempDouble2;
                    if (str2.contains("/")) {
                        tempStr2 = str2.split("/")[0];
                        tempDouble2 = Double.parseDouble(str2.split("/")[1]);
                    } else if (str2.contains("*")) {
                        tempStr2 = str2.split("\\*")[1];
                        tempDouble2 = Double.parseDouble(str2.split("\\*")[0]);
                    } else {
                        tempStr2 = str2;
                        tempDouble2 = 0.0;
                    }

                    if (!tempStr1.equals(tempStr2)) continue;

                    if (str1.contains("/") && str2.contains("/")) {
                        result[i] = tempDouble2/tempDouble1;
                    } else if (str1.contains("/") && str2.contains("*")) {
                        result[i] = 1.0/(tempDouble1*tempDouble2);
                    } else if (str1.contains("*") && str2.contains("/")){
                        result[i] = tempDouble1 * tempDouble2;
                    } else {
                        // 如果有单值：
                        if (tempDouble1 == 0.0) {
                            if (str2.contains("/")) {
                                result[i] = tempDouble2;
                            } else {
                                result[i] = 1.0 / tempDouble2;
                            }
                        } else if (tempDouble2 == 0.0) {
                            if (str1.contains("/")) {
                                result[i] = 1.0 / tempDouble1;
                            } else {
                                result[i] = tempDouble1;
                            }
                        } else {
                            result[i] = tempDouble1/tempDouble2;
                        }
                    }
                }
            }
        }

        return result;
    }


    public static void main(String[] args) {
        List<List<String>> list1 = new ArrayList<>();
        List<String> listTemp1 = new ArrayList<>();
        listTemp1.add("a");
        listTemp1.add("b");
        list1.add(listTemp1);
        List<String> listTemp2 = new ArrayList<>();
        listTemp2.add("b");
        listTemp2.add("c");
        list1.add(listTemp2);

        double[] doubles = new double[list1.size()];
        doubles[0] = 2.0;
        doubles[1] = 3.0;

        List<List<String>> list2 = new ArrayList<>();
        listTemp1 = new ArrayList<>();
        listTemp1.add("a");
        listTemp1.add("c");
        list2.add(listTemp1);
        listTemp2 = new ArrayList<>();
        listTemp2.add("b");
        listTemp2.add("a");
        list2.add(listTemp2);
        List<String> listTemp3 = new ArrayList<>();
        listTemp3.add("a");
        listTemp3.add("e");
        list2.add(listTemp3);
        List<String> listTemp4 = new ArrayList<>();
        listTemp4.add("a");
        listTemp4.add("a");
        list2.add(listTemp4);
        List<String> listTemp5 = new ArrayList<>();
        listTemp5.add("x");
        listTemp5.add("x");
        list2.add(listTemp5);

        for (double v : calcEquation(list1, doubles, list2)) {
            System.out.println(v);
        }
    }
}
